package com.iurac.service.impl;

import com.iurac.dao.BookDao;
import com.iurac.dao.BorrowHistoryDao;
import com.iurac.dao.BorrowDao;
import com.iurac.dao.UserDao;
import com.iurac.domain.Book;
import com.iurac.domain.Borrow;
import com.iurac.domain.BorrowHistory;
import com.iurac.domain.User;
import com.iurac.exception.BorrowException;
import com.iurac.service.BorrowService;
import com.iurac.vo.BorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service("borrowService")
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    BorrowDao borrowDao;
    @Autowired
    BorrowHistoryDao borrowHistoryDao;
    @Autowired
    BookDao bookDao;
    @Autowired
    UserDao userDao;

    @Override
    @Transactional(readOnly = true)
    public List<BorrowVo> getBorrowByCondition(String title, String username, String userId) {
        return borrowDao.getBorrowByCondition(title,username,userId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BorrowException.class)
    public boolean deleteBorrowById(String id) throws BorrowException {
        Borrow borrow = borrowDao.getBorrowById(id);
        Book book = bookDao.getBooksById(borrow.getBookId());
        int result1 = bookDao.updateBookReduceBorrows(book.getId());
        if(result1!=1){
            throw new BorrowException("删除借阅失败");
        }
        int result2 = borrowDao.deleteBorrowById(id);
        if(result2!=1){
            throw new BorrowException("删除借阅失败");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public List<BorrowHistory> getBorrowHistoryByCondition(String title, String username, String userId, String startDate, String endDate) {
        return borrowHistoryDao.getBorrowHistoryByCondition(title,username,userId,startDate,endDate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BorrowException.class)
    public boolean deleteBorrowHistoryById(String id) throws BorrowException {
        int result = borrowHistoryDao.deleteBorrowHistoryById(id);
        if(result!=1){
            throw new BorrowException("删除借阅历史失败");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public List<BorrowVo> getBorrowByUid(String userId) {
        return borrowDao.getBorrowByUid(userId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BorrowException.class)
    public boolean borrowBook(Borrow borrow) throws BorrowException {
        Book book = bookDao.getBooksById(borrow.getBookId());
        Integer stock = Integer.parseInt(book.getStock());
        Integer borrows = Integer.parseInt(book.getBorrows());
        if(stock-borrows <= 0){
            throw new BorrowException("该图书暂无库存");
        }
        User user = userDao.getUserById(borrow.getUserId());
        Integer borrowedCount = Integer.parseInt(userDao.getBorrowCountById(borrow.getUserId()));
        Integer borrowBookLimit = Integer.parseInt(user.getBorrowBookLimit());
        Integer borrowTime = Integer.parseInt(borrow.getBorrowTime());
        Integer borrowTimeLimit = Integer.parseInt(user.getBorrowTimeLimit());
        if(borrowBookLimit-borrowedCount <= 0){
            throw new BorrowException("超出借阅数量限制");
        }
        if(borrowTimeLimit-borrowTime < 0){
            throw new BorrowException("超出借阅天数限制");
        }
        int result1 = borrowDao.borrowBook(borrow);
        int result2 = bookDao.updateBookIncreaseBorrows(borrow.getBookId());
        if(result1!=1 || result2!=1){
            throw new BorrowException("无法添加借阅记录");
        }
        return true;
    }

    @Override
    @Transactional(readOnly = true)
    public List<BorrowVo> getBorrowByAccount(String account) {
        return borrowDao.getBorrowByAccount(account);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BorrowException.class)
    public boolean returnBook(String bid, BorrowHistory borrowHistory) throws BorrowException {
        deleteBorrowById(bid);
        int result = borrowHistoryDao.saveBorrowHistory(borrowHistory);
        if(result!=1){
            throw new BorrowException("添加借阅历史失败");
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = BorrowException.class)
    public boolean renewBook(String id, String renewDay) throws BorrowException {
        Borrow borrow = borrowDao.getBorrowById(id);
        User user = userDao.getUserById(borrow.getUserId());
        Integer borrowTime = Integer.parseInt(renewDay);
        Integer borrowTimeLimit = Integer.parseInt(user.getBorrowTimeLimit());
        if(borrowTimeLimit-borrowTime <= 0){
            throw new BorrowException("超出借阅天数限制");
        }
        int result = borrowDao.renewBook(id,renewDay);
        if(result!=1){
            throw new BorrowException("无法更新借阅记录");
        }
        return true;
    }
}
